#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# 安装SVN自动化部署插件

echo "正在安装SVN自动化部署插件..."

# 检查系统类型和包管理器
if command -v yum &> /dev/null; then
    PM="yum"
elif command -v dnf &> /dev/null; then
    PM="dnf"
elif command -v apt-get &> /dev/null; then
    PM="apt-get"
elif command -v apk &> /dev/null; then
    PM="apk"
else
    echo "错误: 无法识别包管理器，请手动安装SVN"
    exit 1
fi

# 安装SVN
echo "安装SVN服务..."
case "$PM" in
    "yum")
        yum install -y subversion
        ;;
    "dnf")
        dnf install -y subversion
        ;;
    "apt-get")
        apt-get update
        apt-get install -y subversion
        ;;
    "apk")
        apk update
        apk add subversion
        ;;
    *)
        echo "错误: 不支持的包管理器: $PM"
        exit 1
        ;;
esac

# 检查安装结果
if ! command -v svnserve &> /dev/null; then
    echo "SVN安装失败，尝试其他安装方法..."
    
    # 尝试从源码编译安装
    echo "尝试从源码编译安装SVN..."
    cd /tmp
    wget https://archive.apache.org/dist/subversion/subversion-1.14.2.tar.gz
    tar -xzf subversion-1.14.2.tar.gz
    cd subversion-1.14.2
    ./configure --prefix=/usr/local
    make && make install
    
    # 检查编译安装结果
    if ! command -v svnserve &> /dev/null; then
        echo "SVN安装失败，请手动安装SVN服务"
        exit 1
    fi
fi

# 创建插件目录
mkdir -p /www/server/panel/plugin/svn_auto_deploy
mkdir -p /var/svn
mkdir -p /www/server/panel/plugin/svn_auto_deploy/logs

# 设置权限
chmod 755 /www/server/panel/plugin/svn_auto_deploy
chmod 755 /var/svn

echo "SVN自动化部署插件安装完成"
echo "SVN版本: $(svnserve --version | head -n 1)"

exit 0